package com.mxchip.livestar.original.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import com.mxchip.livestar.rx.RxBus;
import com.mxchip.livestar.rx.RxConstant;

import java.text.SimpleDateFormat;
import java.util.TimeZone;

public class NetChangeReceiver extends BroadcastReceiver {

    private static final String TAG = "NetChangeReceiver";
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e(TAG, "onReceive: "+"" );

        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));

        String action=intent.getAction();
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (manager != null) {
            NetworkInfo[] infos = manager.getAllNetworkInfo();
            if (infos != null) {
                for (int i = 0; i < infos.length; i++) {
                    //判断有线网络
                    if (infos[i].getType() == ConnectivityManager.TYPE_ETHERNET) {

                        NetworkInfo.State state = infos[i].getState();
                        if (NetworkInfo.State.CONNECTED == state) {
                            RxBus.get().post(RxConstant.INSERT_NET,true);
//                            logger.info(simpleDateFormat.format(new Date()) + " NetworkInfo.State:" + state + "  \r\n");
//                            RxBus.get().post(RxConstant.CHANGE_NET_STATE,true);
                        } else {
//                              RxBus.get().post(RxConstant.CHANGE_NET_STATE,false);
                        }
                    }
                }
            }
        }
    }
}
